package com.sgam.mf.domain.stock;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityResult;
import javax.persistence.FieldResult;
import javax.persistence.Id;
import javax.persistence.NamedNativeQuery;
import javax.persistence.SqlResultSetMapping;
import javax.persistence.Transient;

import org.hibernate.annotations.Formula;



////@SqlResultSetMapping (name="userSold", entities=@EntityResult(entityClass=StockSold.class))
//@NamedNativeQuery(

@SqlResultSetMapping (name="userSold",  
					entities=@EntityResult(entityClass=StockSold.class, 
					fields={ @FieldResult(name="id", 		column="id"),
							 @FieldResult(name="denumire", 	column="denumire"),
							 @FieldResult(name="dtSold", 	column="dt_sold"),
							 @FieldResult(name="ctSold", 	column="ct_sold")
								}))
@NamedNativeQuery(
		resultSetMapping = "userSold",
		name = "getStockSoldByForUserByDate",
		query = " SELECT " +
					" sold.ref_gift_fk AS id, " +
					" (SELECT gft.md_value FROM ref_gift gft WHERE sold.ref_gift_fk = gft.id) AS denumire, " +
					" sum(sold.dt_rulaj) AS dt_sold, " +
					" sum(sold.ct_rulaj) AS ct_sold " +
				" FROM (  SELECT " +
							" ref_gift_fk, " +
							" cantitate   AS dt_rulaj, " +
							" 0           AS ct_rulaj " +
						" FROM V_OPERATIONS " +
						" WHERE   document_data   <= 	:SOLD_DATA " +
						" AND   	dt_sec_user_fk = 	:USER_ID " +
						" UNION ALL " +
						" SELECT " +
							" ref_gift_fk, " +
							" 0           AS dt_rulaj, " +
							" cantitate   AS ct_rulaj " +
						" FROM V_OPERATIONS " +
						" WHERE   document_data   <= 	:SOLD_DATA " +
						" AND   	ct_sec_user_fk = 	:USER_ID " +
						" ) sold " +
				" GROUP BY  ref_gift_fk  "
)
@Entity
public class StockSold {


	@Id
	@Column(name = "ID")
	private Integer id;
     
	@Column(name = "DENUMIRE")
	private String denumire;

	@Column(name = "DT_SOLD")
	private Integer dtSold;
	
	@Column(name = "CT_SOLD")
	private Integer ctSold;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getDenumire() {
		return denumire;
	}

	public void setDenumire(String denumire) {
		this.denumire = denumire;
	}

	public Integer getDtSold() {
		return dtSold;
	}

	public void setDtSold(Integer dtSold) {
		this.dtSold = dtSold;
	}

	public Integer getCtSold() {
		return ctSold;
	}

	public void setCtSold(Integer ctSold) {
		this.ctSold = ctSold;
	}

	public Integer getTotal() {
		return dtSold - ctSold;
	}
	
	
}
